<html xmlns:th="http://www.thymeleaf.org">
<head>
    <meta charset="utf-8">
    <title>Layui</title>
    <meta name="renderer" content="webkit">
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1">
    <meta name="viewport" content="width=device-width, initial-scale=1, maximum-scale=1">
    <link rel="stylesheet" th:href="@{/layui/css/layui.css}"  media="all">
    <script th:src="@{/layui/layui.js}" charset="utf-8"></script>

</head>
<body>
<br>
<form class="layui-form" id="seachForm">
    <div class="layui-form-item">
        <div class="layui-inline">
            <label class="layui-form-label">用户名</label>
            <div class="layui-input-inline">
                <input type="text" name="name" id="name"  autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <label class="layui-form-label">订单号</label>
            <div class="layui-input-inline">
                <input type="text" name="orderid" id="orderid" autocomplete="off" class="layui-input">
            </div>
        </div>
        <div class="layui-inline">
            <button type="button" onclick="seachOn();" class="layui-btn" >搜索</button>
            <!--<button type="reset" class="layui-btn layui-btn-primary">重置</button>-->
        </div>
    </div>
</form>

<table class="layui-hide" id="test" lay-filter="test"></table>

<script type="text/html" id="toolbarDemo">
    <div class="layui-btn-container">
        <button class="layui-btn layui-btn-sm" lay-event="getCheckData">获取选中行数据</button>
        <button class="layui-btn layui-btn-sm" lay-event="getCheckLength">获取选中数目</button>
        <button class="layui-btn layui-btn-sm" lay-event="isAll">验证是否全选</button>
        <button class="layui-btn layui-btn-sm" lay-event="add">新增</button>
    </div>
</script>
<script type="text/html" id="barDemo">
    <a class="layui-btn layui-btn-xs" lay-event="edit">编辑</a>
    <a class="layui-btn layui-btn-danger layui-btn-xs" lay-event="del">删除</a>
</script>

<script>
    var $,table;
    layui.use(['table','layer','jquery'], function(){
        $=layui.jquery;
        table = layui.table;
        table.render({
            elem: '#test'
            ,url:'/courseorder/listjson'
            ,toolbar: '#toolbarDemo'
            ,title: '用户数据表'
            ,totalRow: true
            ,cols: [
                [
                {type: 'checkbox', fixed: 'left'}
                //,{field:'uuid', title:'ID', width:80, fixed: 'left', unresize: true, sort: true, totalRowText: '合计'}
                ,{field:'name', title:'用户名', width:120}
                ,{field:'orderid', title:'订单号', width:120}
                ,{field:'createdata', title:'创建时间', width:120}
                ,{field:'memo', title:'备注', width:120}
                ,{fixed: 'right', title:'操作', toolbar: '#barDemo', width:150}
            ]
            ]
            ,page: true
            ,request:{
                pageName:'pn' //页码参数名称
                ,limitName:'size' //每页数据量的参数名
            }
            ,id:'orderTable'
        });

        //工具栏事件
        table.on('toolbar(test)', function(obj){
            var checkStatus = table.checkStatus(obj.config.id);
            switch(obj.event){
                case 'getCheckData':
                    var data = checkStatus.data;
                    layer.alert(JSON.stringify(data));
                    break;
                case 'getCheckLength':
                    var data = checkStatus.data;
                    layer.msg('选中了：'+ data.length + ' 个');
                    break;
                case 'isAll':
                    layer.msg(checkStatus.isAll ? '全选': '未全选')
                    break;
                case 'add':
                    parent.layer.open({
                        type: 2 //此处以iframe举例
                        ,offset: 'auto'
                        ,title: '添加'
                        ,content: '/courseorder/savePage'
                        ,area: ['1000px', '600px']
                        ,btn: '全部关闭'
                        ,btnAlign: 'c'
                        ,btn2: function(){
                            parent.layer.closeAll();
                        }
                    });
                    break;
            };
        });

        //监听行双击事件
        table.on('rowDouble(test)', function(obj){
            //obj 同上
            var uuid=obj.data.uuid;
            parent.layer.open({
                type: 2 //此处以iframe举例
                ,offset: 'auto'
                ,title: '添加'
                ,content: '/courseorder/updatePage?uuid='+uuid
                ,area: ['1000px', '600px']
                ,btn: '全部关闭'
                ,btnAlign: 'c'
                ,btn2: function(){
                    parent.layer.closeAll();
                }
            });
        });

        //监听工具条
        table.on('tool(test)', function(obj){ //注：tool 是工具条事件名，test 是 table 原始容器的属性 lay-filter="对应的值"
            var data = obj.data; //获得当前行数据
            var layEvent = obj.event; //获得 lay-event 对应的值（也可以是表头的 event 参数对应的值）
            var tr = obj.tr; //获得当前行 tr 的 DOM 对象（如果有的话）
            var uuid=data.uuid;

            if(layEvent === 'del'){ //删除
                layer.confirm('真的删除行么', function(index){
                    $.ajax({
                        type:"post",url: "/courseorder/delete?uuid="+uuid,
                        //data:uuid,
                        error:function () {
                            layer.alert("操作失败，请您稍后再试！");
                        },
                        success:function (msg) {
                            if(msg=='true'){
                                layer.msg("操作成功", {
                                    icon: 1,//成功的表情
                                    time: 1000 //1秒关闭（如果不配置，默认是3秒）
                                }, function(){
                                    parent.layer.closeAll();
                                    //修改成功后刷新父界面
                                    window.parent.location.reload();
                                });
                            }else{
                                layer.alert("操作失败，请您稍后重试！");
                            }
                        }
                    });
                });
            } else if(layEvent === 'edit'){ //编辑
                parent.layer.open({
                    type: 2 //此处以iframe举例
                    ,offset: 'auto'
                    ,title: '添加'
                    ,content: '/courseorder/updatePage?uuid='+uuid
                    ,area: ['1000px', '600px']
                    ,btn: '全部关闭'
                    ,btnAlign: 'c'
                    ,btn2: function(){
                        parent.layer.closeAll();
                    }
                });
            }
        });

    });

    function seachOn() {
        var name = $("#name").val();
        var orderid = $("#orderid").val();
        table.reload('orderTable',{
            where: {
                name: name,
                orderid: orderid
            }
        });
    }
</script>

</body>
</html>